package com.ysq.www.api.front.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ysq.www.api.front.entity.Press;
import com.ysq.www.api.front.service.IPressService;
import com.ysq.www.common.annotation.AuthToken;
import com.ysq.www.common.result.ResultMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 新闻表 前端控制器
 * </p>
 *
 * @author dql
 * @since 2020-08-07
 */
@Slf4j
@RestController
@RequestMapping("/front/press")
public class PressController {

    @Autowired
    private IPressService pressService;

    /**
     * 查询顶置新闻
     * @return
     */
    @GetMapping("/search")
    @AuthToken
    public ResultMsg search(){
        QueryWrapper<Press> wrapper = new QueryWrapper<>();
        wrapper.select("id","name","type","creator","tag").
                eq("overhead",1).eq("status",0).last("limit 2");
        List<Press> pList = pressService.list(wrapper);
        return ResultMsg.SUCCESS.setNewMsg("查询成功").setNewData(pList);
    }


    /**
     * 根据新闻id查询新闻详情
     */
    @GetMapping("/details")
    @AuthToken
    public ResultMsg selectDetailsById(Integer id){
        QueryWrapper<Press> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id).eq("status",0);
        Press press = pressService.getOne(wrapper);
        return ResultMsg.SUCCESS.setNewMsg("查询成功").setNewData(press);
    }


    /**
     * 分页查询今日头条新闻
     * @param pageNumber 页码
     * @param pageSize 页大小
     * @return ResultMsg
     */
    @GetMapping("/page")
    @AuthToken
    public ResultMsg selectPressByPage(Integer pageNumber,Integer pageSize){
        IPage<Press> pPage = pressService.selectPressByPage(pageNumber,pageSize);
        return ResultMsg.SUCCESS.setNewMsg("查询成功").setNewData(pPage);
    }

}
